﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Common;
using SLMF.Utility;
using SLMF.Business;
using SLMF.Entity;

public partial class SlmControls_slm_forum : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Params["groupid"] != null)
            {
                int intCateId = int.Parse(Request.Params["groupid"].ToString());
                BindCateId(intCateId);
            }
            else
            {
                BindCategory();
                lblGroupName.Visible = false;
                lblSpace.Visible = false;
            }
            MembersOnline();
            LoadOnlineDetails();
            LoadAnalytics();
            FastLogin();
            btnLogin.Attributes.Add("onclick", "GetSlmEnc();");
            ReviewCookieMember();
        }
    }

    private void FastLogin()
    {
        txtUser.ToolTip = LoadSLMF("SLMF_FastLog", "User");
        txtPass.ToolTip = LoadSLMF("SLMF_FastLog", "Pass");
        ckbRemember.ToolTip = LoadSLMF("SLMF_FastLog", "SavePass");
        btnLogin.Text = LoadSLMF("SLMF_FastLog", "Login");
        rfv1.ToolTip = LoadSLMF("SLMF_FastLog", "Rfv1");
        rfv2.ToolTip = LoadSLMF("SLMF_FastLog", "Rfv2");
    }

    private void MembersOnline()
    {
        EnOnlines r = BuInformation.LstSelMemberOnline();
        activeuser.Text = "Kỷ lục: " + r.F4.ToString() + " người dùng online vào ngày " +
            r.F5.Day.ToString() + "/" + r.F5.Month.ToString() + "/" + r.F5.Year.ToString() + " lúc " +
            r.F5.ToShortTimeString() + ".<br/>" +
            "<a href=\"onlineuser.aspx\" class=\"TopicLink\" >" + LoadSLMF("SLMF_Forum", "TotalOnlines") + "</a> " +
            r.F1.ToString() + " - " + LoadSLMF("SLMF_Forum", "TotalGuests") + r.F3.ToString()+ ", " + 
            LoadSLMF("SLMF_Forum", "TotalMembers") + r.F2.ToString();
    }

    private void LoadOnlineDetails()
    {
        SqlDataReader r = BuInformation.SelectMemberDetails();
        listuser.DataSource = r;
        listuser.DataBind();
        if (r.IsClosed == false)
        {
            r.Close();
            r.Dispose();
        }
    }

    private void LoadAnalytics()
    {
        EnStats r = BuForum.LstselAnal();
        System.Text.StringBuilder anal = new System.Text.StringBuilder(450);
        if (r != null)
        {
            anal.Append("Có tất cả: <b>" + r.Members.ToString() + //r["Members"].ToString() + 
                "</b> thành viên đã gửi: <b>" + r.Messages.ToString() + "</b> bài viết trong tổng số: <b>" + r.Forums.ToString() + "</b> diễn đàn. ");
            anal.Append("Hiện tại có: <b>" + r.Topics.ToString() + "</b> chủ đề.<br/>");
            anal.Append("Hân hạnh chào đón thành viên mới nhất: <b>" + "<a class=\"TopicLink\" href=\"profile/" + r.UserName.ToString() + "-" + r.MemberId.ToString() + ".aspx" + "\">" + r.UserName.ToString() + "</a></b><br/>");
            if (r.NewPost.ToString() != "")
            {
                DateTime dt = new DateTime();
                dt = DateTime.Parse(r.NewPost.ToString());
                if (r.NewMember != null) 
                    anal.Append("Bài viết mới nhất gửi bởi: <b>" + "<a class=\"TopicLink\" href=\"profile/" 
                        + r.NewMember.ToString() + "-" + r.NewMemberId.ToString() + ".aspx" + "\">" 
                        + r.NewMember.ToString() + "</a></b> " + "vào lúc: <b>" + dt.Day.ToString() 
                        + "/" + dt.Month.ToString() + "/" + dt.Year.ToString() + " " + dt.ToShortTimeString() + "</b><br/>");
            }
            anal.Append("Bạn có thể vào đây để xem: " + "<img align=\"absmiddle\" src=\"slmimages/pr7.gif\"/> <b><a class=\"TopicLink\" href=\"listactive.aspx" + "\">" +
                             "Xem bài viết hôm nay" + "</a></b> | " + "<img align=\"absmiddle\" src=\"slmimages/pr8.gif\"/> <b><a class=\"TopicLink\" href=\"active/week.aspx" + "\">" +
                             "Xem bài viết trong tuần" + "</a></b> | " + "<img align=\"absmiddle\" src=\"slmimages/pr9.gif\"/> <b><a class=\"TopicLink\" href=\"active/month.aspx" + "\">" +
                             "Xem bài viết trong tháng" + "</a></b>");
        }
        stats.Text = anal.ToString();
    }

    private void BindCateId(int intCategoryId)
    {
        EnCategory cate = new EnCategory();
        cate.CategoryId = intCategoryId;
        System.Collections.Generic.List<EnCategory> lsc = BuCategory.LstCategoryFId(cate);
        rptCategory.DataSource = lsc;
        rptCategory.DataBind();
        lblGroupName.Text = lsc[0].CateName;
        lblGroupName.Visible = true;
        Page.Title = lsc[0].CateName + ". " + LoadSLMF("TitleOfPage", "JNTPAF");
    }

    private void BindCategory()
    {
        rptCategory.DataSource = BuCategory.LstCategory();
        rptCategory.DataBind();
    }

    private SqlDataReader BindForum(int intCategoryId)
    {
        EnCategory category = new EnCategory();
        category.CategoryId = intCategoryId;
        EnMember mbr = new EnMember();
        mbr.UserName = SentForCook();
        SqlDataReader datrForum = BuForum.SelectForum(category, mbr);
        return datrForum;
    }

    public string LoadSLMF(string strA, string strB)
    {
        UtiGeneralClass slmNew = new UtiGeneralClass();
        string strOutput = slmNew.LoadLangs(strA, strB);
        return strOutput;
    }

    protected void rptCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            int i = ((EnCategory)e.Item.DataItem).CategoryId;
            Repeater rptForumNew = new Repeater();
            rptForumNew = (Repeater)e.Item.FindControl("rptForum");
            SqlDataReader datrForum = BindForum(i);
            rptForumNew.DataSource = datrForum;
            rptForumNew.DataBind();
            if (datrForum.IsClosed == false)
            {
                datrForum.Close();
                datrForum.Dispose();
            }
        }
    }
    protected void rptForum_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            int i = int.Parse(((DbDataRecord)e.Item.DataItem)[0].ToString());
            EnForum forum = new EnForum();
            forum.ForumId = i;
            EnForum newforum = new EnForum();
            EnTopic topic = new EnTopic();
            EnMessage newmessage = new EnMessage();
            EnMember newmember = new EnMember();
            int intResult = 0;
            newforum = BuForum.SelectForumDetails(ref intResult, forum, ref topic, ref newmessage, ref newmember);
            Repeater rptModNew = new Repeater();
            rptModNew = (Repeater)e.Item.FindControl("rptModerator");
            int j = 0;
            EnModerator moderator = new EnModerator();
            moderator.ForumId = i;
            System.Collections.Generic.List<EnModerator> datrModer = BuModerator.LstSelModerator(moderator, ref j);
            if (datrModer != null && datrModer.Count > 0)
            {
                rptModNew.DataSource = datrModer;
                rptModNew.DataBind();
            }
            Repeater rptSubNew = new Repeater();
            rptSubNew = (Repeater)e.Item.FindControl("rptSubForum");
            int k = 0;
            System.Collections.Generic.List<EnForum> datrSub = BuForum.LstSelectSubF(forum, ref k);
            if (datrSub != null && datrSub.Count >0)
            {
                rptSubNew.DataSource = datrSub;
                rptSubNew.DataBind();
            }
            int viewing = BuInformation.SelectForumVieing(forum);
            if (viewing > 0)
            {
                Label lblVNew = new Label();
                lblVNew = (Label)e.Item.FindControl("lblViewing");
                lblVNew.Text = "[" + viewing.ToString() + " đang xem]";
            }
            UtiGeneralClass slmNew = new UtiGeneralClass();
            if (intResult == 1)
            {
                Label lbl1new = new Label();
                lbl1new = (Label)e.Item.FindControl("lblTotalTopics");
                lbl1new.Text = newforum.TotalTopics.ToString();
                Label lbl2new = new Label();
                lbl2new = (Label)e.Item.FindControl("lblTotalMessages");
                lbl2new.Text = newforum.TotalMessages.ToString();
                HtmlAnchor hrfMsgNew = new HtmlAnchor();
                hrfMsgNew = (HtmlAnchor)e.Item.FindControl("hrfMessage");
                string strTitle = newmessage.Title.ToString();
                UtiString utinew = new UtiString();
                int intLimit = int.Parse(slmNew.LoadLangs("SLMF_Forum", "LenghtString").ToString());
                strTitle = utinew.ReleaseInput(strTitle, intLimit).Trim();
                hrfMsgNew.InnerText = strTitle;
                hrfMsgNew.Title = newmessage.Title.ToString();
                hrfMsgNew.HRef = "~/Forum/" + SLMF.Utility.UtiString.cnf(strTitle) + "_" + newmessage.MessageId.ToString() + ".aspx#" + newmessage.MessageId.ToString();
                Label lbl3new = new Label();
                lbl3new = (Label)e.Item.FindControl("lblTime");
                string strTime = utinew.TimeServer(newmessage.CreationDate);
                lbl3new.Text = "<br/>" + strTime + "<br/>";
                string ck = "";
                bool bolIcon = false;
                ck = SentForCook();
                if (ck != "")
                {
                    bolIcon = BuInformation.SelectPairTime(2, ck, newmessage.CreationDate);
                }
                else
                {
                    string strCur = HttpContext.Current.Session.SessionID;
                    bolIcon = BuInformation.SelectPairTime(1, strCur, newmessage.CreationDate);
                }
                if (bolIcon)
                {
                    HtmlImage iconNew = new HtmlImage();
                    iconNew = (HtmlImage)e.Item.FindControl("ifrm");
                    iconNew.Src = "../SlmImages/topic_new.png";
                }
                HtmlAnchor hrfMemberNew = new HtmlAnchor();
                hrfMemberNew = (HtmlAnchor)e.Item.FindControl("hrfMember");
                hrfMemberNew.InnerText = newmember.UserName.ToString();
                hrfMemberNew.HRef = "~/Forum/profile/" + newmember.UserName.ToString() + "-" + newmember.MemberId.ToString() + ".aspx";
                HtmlAnchor hrfnewmNew = new HtmlAnchor();
                hrfnewmNew = (HtmlAnchor)e.Item.FindControl("hrfnewm");
                hrfnewmNew.Visible = true;
                hrfnewmNew.Title = LoadSLMF("SLMF_Forum", "GoToNewPost");
                hrfnewmNew.HRef = "~/Forum/" + SLMF.Utility.UtiString.cnf(strTitle) + "_" + newmessage.MessageId.ToString() + ".aspx#" + newmessage.MessageId.ToString();
            }
            else
            {
                Label lbl1new = new Label();
                lbl1new = (Label)e.Item.FindControl("lblTotalTopics");
                lbl1new.Text = newforum.TotalTopics.ToString();
                Label lbl2new = new Label();
                lbl2new = (Label)e.Item.FindControl("lblTotalMessages");
                lbl2new.Text = newforum.TotalMessages.ToString();
                Label lbl3new = new Label();
                lbl3new = (Label)e.Item.FindControl("lblTime");
                lbl3new.Text = slmNew.LoadLangs("SLMF_Forum", "NotHaveForum");
                HtmlAnchor hrfnewmNew = new HtmlAnchor();
                hrfnewmNew = (HtmlAnchor)e.Item.FindControl("hrfnewm");
                hrfnewmNew.Visible = false;
            }
        }
    }

    private string SentForCook()
    {
        HttpCookie SlmMemberCookie = new HttpCookie("SLMFMembers");
        SlmMemberCookie = Request.Cookies["SLMFMembers"];
        string strUser = "";
        if (SlmMemberCookie != null && SlmMemberCookie.Value != "" &&
             SlmMemberCookie.Value != null)
        {
            strUser = SlmMemberCookie.Value.ToString();
        }
        return strUser;
    }

    public string GetTextTotal(string strInput)
    {
        UtiGeneralClass slmNew = new UtiGeneralClass();
        string strOutput = "";
        if (strInput == "TotalTocpicSub")
        {
            strOutput = slmNew.LoadLangs("SLMF_Forum", "TotalTocpicSub");
        }
        else if (strInput == "TotalMessSub")
        {
            strOutput = slmNew.LoadLangs("SLMF_Forum", "TotalMessSub");
        }
        return strOutput;
    }
    protected void listuser_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            int i = int.Parse(((DbDataRecord)e.Item.DataItem)[0].ToString());
            EnMember mbr = new EnMember();
            mbr.MemberId = i;
            if (!BuMember.SelectMemberGuest(mbr))
            {
                listuser.Visible = true;
                HtmlAnchor n = new HtmlAnchor();
                n = (HtmlAnchor)e.Item.FindControl("hrfUserOnline");
                n.HRef = "~/Forum/profile/" + ((DbDataRecord)e.Item.DataItem)[1].ToString() + "-" + i + ".aspx";
                n.InnerHtml = ((DbDataRecord)e.Item.DataItem)[1].ToString();
                if (((DbDataRecord)e.Item.DataItem)[4].ToString() == "" && ((DbDataRecord)e.Item.DataItem)[5].ToString() == "")
                {
                    if (Request.Params["GroupId"] == null)
                    {
                        n.Title = "Đang ở Trang chủ Diễn đàn";
                        n.Title += " - Online: " + ((DbDataRecord)e.Item.DataItem)[10] + " phút";
                    }
                    else
                    {
                        EnCategory c = new EnCategory();
                        c.CategoryId = int.Parse(Request.QueryString["GroupId"].ToString());
                        string strCateName = BuCategory.SelectCateName(c);
                        n.Title = "Đang xem Nhóm Diễn đàn: [" + strCateName;
                        n.Title += "] - Online: " + ((DbDataRecord)e.Item.DataItem)[10] + " phút";
                    }
                }
                else if (((DbDataRecord)e.Item.DataItem)[4].ToString() == "" && ((DbDataRecord)e.Item.DataItem)[5].ToString() != "")
                {
                    n.Title = "Đang ở Diễn đàn: " + ((DbDataRecord)e.Item.DataItem)[6];
                    n.Title += " - Online: " + ((DbDataRecord)e.Item.DataItem)[10] + " phút";
                }
                else if (((DbDataRecord)e.Item.DataItem)[4].ToString() != "" && ((DbDataRecord)e.Item.DataItem)[5].ToString() != "")
                {
                    n.Title = "Đang xem Chủ đề: " + ((DbDataRecord)e.Item.DataItem)[7] + 
                                " tại Diễn đàn: " + ((DbDataRecord)e.Item.DataItem)[6];
                    n.Title += " - Online: " + ((DbDataRecord)e.Item.DataItem)[10] + " phút";
                }
            }
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        MemberLoginTry();
    }
    private void MemberLoginTry()
    {
        UtiGeneralClass error = new UtiGeneralClass();
        string strUserName = txtUser.Text.ToString();
        string strPassword = txtPass.Text.ToString();
        if (strUserName != "" && strPassword != "")
        {
            int intResult = 0;
            EnMember member = new EnMember();
            member.UserName = strUserName;
            intResult = BuMember.MemberLogin(member);
            if (intResult != -1 && intResult != -2 && intResult != -3 && intResult != -4 && intResult != -5)
            {
                string strPass = "", strSalt = "";
                strSalt = member.Salt;
                strPass = member.NewPassword;

                string strMd5Client = slmhas.Value.ToString();
                UtiHashMd5 md5 = new UtiHashMd5();
                string strMd5Server = md5.Md5Encode(strMd5Client + strSalt);

                if (strMd5Server == strPass)
                {
                    int intReport = 0;
                    EnMemberProfile memberprofile = new EnMemberProfile();
                    memberprofile.IP = Request.ServerVariables["REMOTE_ADDR"];
                    intReport = BuMemberProfile.LoginMemberSuccess(member, memberprofile);
                    if (intReport == 1)
                    {
                        member.Password = strMd5Server;
                        SetCookieMember(member);
                        Response.Redirect("Default.aspx");
                    }
                }
            }
        }
    }
    private void SetCookieMember(EnMember member)
    {
        HttpCookie SlmMemberCookie = new HttpCookie("SLMFMembers");
		HttpCookie SlmSessionId = new HttpCookie("SLMFSessionId");
        DateTime dateSLMF = DateTime.Now;
        SlmMemberCookie.Value = member.UserName;
        SlmMemberCookie.Expires = dateSLMF.AddDays(3);
		SlmSessionId.Value = member.Password;
        SlmSessionId.Expires = dateSLMF.AddDays(4);
        if (ckbRemember.Checked)
        {
            SlmMemberCookie.Expires = dateSLMF.AddDays(7);
			SlmSessionId.Expires = dateSLMF.AddDays(8);
        }
        Response.Cookies.Add(SlmMemberCookie);
		Response.Cookies.Add(SlmSessionId);
    }

    private void ReviewCookieMember()
    {
        HttpCookie SlmMemberCookie = new HttpCookie("SLMFMembers");
        SlmMemberCookie = Request.Cookies["SLMFMembers"];
        if (SlmMemberCookie != null && SlmMemberCookie.Value != "" &&
             SlmMemberCookie.Value != null)
        {
            pnlFastLog.Visible = false;
        }
        else
        {
            pnlFastLog.Visible = true;
        }
    }
}